<template>
  <div class="list">
    <!-- 面包屑导航 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
     <el-row :gutter="10">
       <el-col :span="8">
         <el-input placeholder="请输入内容" v-model="queryInfo.query" clearable @clear="getGoodsList" @keyup.enter.native="getGoodsList">
           <el-button slot="append" icon="el-icon-search" @click.enter="getGoodsList"></el-button>
         </el-input>
       </el-col>
       <el-col :span="4">
         <el-button type="primary" @click="addGoods">添加商品</el-button>
       </el-col>
     </el-row>
     <el-table
      :data="goodsList"
      stripe border
    >
      <el-table-column type="index" align="center"></el-table-column>
      <el-table-column
        prop="goods_name"
        label="商品名称"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="goods_price"
        label="商品价格（元）"
        width="115px"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="goods_weight"
        label="商品重量"
        width="80px"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="upd_time"
        label="创建时间"
        width="180px"
        align="center"
      >
        <template slot-scope="scope">
          {{ scope.row.upd_time*1000 | dataForm}}
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        width="130px"
        align="center"
      >
      <template slot-scope="scope">
        <el-button type="primary" icon="el-icon-edit" size="mini"></el-button>
        <el-button type="danger" icon="el-icon-delete" size="mini" @click="removeGood(scope.row.goods_id)"></el-button>
      </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="queryInfo.pagenum"
      :page-sizes="[10, 15, 20, 40]"
      :page-size="queryInfo.pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  data () {
    return {
      goodsList: [],
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 10
      },
      total: 0,
      JsonGoods: []
    }
  },
  created () {
    this.getGoodsList()
    if (sessionStorage.getItem('good')) {
      this.JsonGoods = JSON.parse(sessionStorage.getItem('good'))
    }
  },
  methods: {
    async getGoodsList () {
      const { data: res } = await this.$http.get('goods', { params: this.queryInfo })
      if (res.meta.status !== 200) return this.$message.error('获取商品列表失败！')
      this.goodsList = res.data.goods
      this.JsonGoods.map((item, index) => {
        this.goodsList[index] = item
      })
      this.total = res.data.total
      console.log(this.goodsList)
    },
    // 监听 pagesize 改变
    handleSizeChange (newsize) {
      this.queryInfo.pagesize = newsize
      this.getGoodsList()
    },
    // 监听 pagenum 改变
    handleCurrentChange (newnum) {
      this.queryInfo.pagenum = newnum
      this.getGoodsList()
    },
    // 根据 id 删除商品
    async removeGood (id) {
      const result = await this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      if (result !== 'confirm') return this.$message.info('已取消删除')
      const { data: res } = await this.$http.delete(`goods/${id}`)
      if (res.meta.status !== 200) return this.$message.error('删除失败')
      this.$message.success('删除成功')
      this.getGoodsList()
    },
    // 通过编程式导航跳转到新的路由
    addGoods () {
      this.$router.push('/goods/add')
    }
  }
}
</script>

<style>

</style>
